package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.model.TbProcessGroup;
import com.tju.resource.business.tb_process_group.TbProcessGroupService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 工艺路线管理接口
 */
@RestController
public class TbProcessGroupController {
    private static final String prefix = "craft";

    @Autowired
    private TbProcessGroupService tbProcessGroupService;

    /**
     * 添加工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @PostMapping(prefix + "/addProcessGroup.do")
    @PreAuthorize("hasAuthority('sys:craft:course:add')")
    @Log(type= FunctionType.Add)
    public Result<Object> addProcessGroup(@Valid @RequestBody TbProcessGroup tbProcessGroup, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbProcessGroupService.addProcessGroup(tbProcessGroup);
    }

    /**
     * 删除工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @PostMapping(prefix + "/deleteProcessGroup.do")
    @Log(type= FunctionType.Delete)
    @PreAuthorize("hasAuthority('sys:craft:course:delete')")
    public Result<Object> deleteProcessGroup(@RequestBody TbProcessGroup tbProcessGroup) {
        return tbProcessGroupService.deleteProcessGroup(tbProcessGroup);
    }

    /**
     * 修改工艺流程管理信息
     *
     * @param tbProcessGroup
     * @return
     */
    @PostMapping(prefix + "/modifyProcessGroup.do")
    @PreAuthorize("hasAuthority('sys:craft:course:edit')")
    @Log(type= FunctionType.Modify)
    public Result<Object> modifyProcessGroup(@Valid @RequestBody TbProcessGroup tbProcessGroup, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbProcessGroupService.modifyProcessGroup(tbProcessGroup);
    }

    /**
     * 分页查询全部工艺流程管理信息
     *
     * @param parameter
     * @return
     */
    @Log(type= FunctionType.List)
    @PostMapping(prefix + "/getProcessGroup.do")
    @PreAuthorize("hasAuthority('sys:craft:course:list')")
    public Result<Object> getProcessGroup(@RequestBody ClientParameters parameter) {
        return tbProcessGroupService.getProcessGroup(parameter);
    }

}
